import {Component, OnInit} from '@angular/core';
import {Router} from "@angular/router";
import {AlertComponent} from "../../unit/alert";
import {environment} from "../../../environments/environment";

@Component({
    selector: 'app-qr-code',
    templateUrl: './qr-code.page.html',
    styleUrls: ['./qr-code.page.scss'],
})
export class QrCodePage implements OnInit {
    title = 'app';
    elementType = 'url';
    codeValue = '';
    currentTime: number;
    currentCard: string;
    currentUrl = environment.basicUrl;

    constructor(private router: Router,
                private alertComponent: AlertComponent,) {
    }

    ngOnInit() {
        this.currentCard = localStorage.getItem('cardNumber');
        if(!this.currentCard){
            this.alertComponent.presentAlert('登录信息过期，请重新登录！').then();
            this.router.navigate(['/login']).then();
        }
        this.initQRCode();
    }

    initQRCode() {
        this.currentTime = new Date().getTime();
        this.codeValue = `${this.currentUrl}/#/code-info?idCard=${this.currentCard}&time=${this.currentTime}`;
    }

    refreshQrCode() {
        this.initQRCode();
    }

    showCodeInfo() {
        const currentCard = localStorage.getItem('cardNumber');
        this.router.navigate([`/code-info`], {queryParams: {idCard: currentCard}}).then();
    }

}
